package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.conmmon.R;
import com.qf.entity.Permission;
import com.qf.entity.User;
import com.qf.service.IAutoService;
import com.qf.service.IPermissionService;
import com.qf.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @propram: IntelliJ IDEA
 * @description:
 * @author: 秦先发
 * @create: 2021-09-27 19:15
 **/
@RequestMapping("/auto")
@RestController
public class AutoController {
    @Autowired
    private IAutoService autoService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IPermissionService permissionService;

    /**
     * 查所有简单JSON
     */
    @PostMapping("/selectAllJson")
    public R selectAllJson() {
        return R.saveR("data", autoService.selectAllJson());
    }

    /**
     * 根据id查询用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/selectPermissionById")
    public R selectPermissionById(@RequestParam Integer id) {
        return R.saveR("data", autoService.getById(id));
    }

    @PostMapping("/selectRoleUser/{rid}")
    public R selectRoleUser(@PathVariable Integer rid) {
        return R.saveR("data", autoService.selectRoleUser(rid));
    }

    @PostMapping("/deleteUsers/{rid}")
    public R deleteUsers(@PathVariable Integer rid, @RequestBody List<User> users) {
        return R.getR(autoService.deleteUsers(rid, users));
    }

    @PostMapping("/addRoleByUser/{rid}")
    public R addRoleByUser(@PathVariable Integer rid, @RequestBody List<User> users) {
        autoService.deleteUsers(rid, users);
        return R.getR(autoService.addRoleByUser(rid, users));
    }

    @PostMapping("/selectPermissionByRid/{rid}")
    public R selectPermissionByRid(@PathVariable Integer rid) {
        R r = R.saveR("haveData", autoService.selectPermissionByRid(rid));
        r.put("data", permissionService.list());
        return r;
    }
    @PostMapping("/deleteRoleByPermission/{rid}")
    public R deleteRoleByPermission(@PathVariable Integer rid) {
        return R.getR(autoService.deleteRoleByPermission(rid));
    }

    @PostMapping("/addRoleByPermission/{rid}")
    public R addRoleByPermission(@PathVariable Integer rid, @RequestBody List<Integer> pids) {
        autoService.deleteRoleByPermission(rid);
        return R.getR(autoService.addRoleByPermission(rid,pids));
    }
}
